package com.wisezone.permission.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.wisezone.common.exception.WzServiceException;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.service.impl.BaseServiceImpl;
import com.wisezone.permission.bean.entity.WzGroupEntity;
import com.wisezone.permission.bean.entity.WzRoleGroupEntity;
import com.wisezone.permission.bean.search.WzGroupSearch;
import com.wisezone.permission.bean.search.WzUserGroupSearch;
import com.wisezone.permission.dao.WzGroupDao;
import com.wisezone.permission.service.WzGroupService;
import com.wisezone.permission.service.WzRoleGroupService;

/**
 * 
 *用户组; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:41 update date 2010-07-29
 *         14:44:41
 * 
 */
public class WzGroupServiceImpl extends BaseServiceImpl implements
		WzGroupService {

	/**
	  *
	  *
	  *
	  */
	private WzGroupDao wzGroupDao;

	private WzRoleGroupService wzRoleGroupService;

	/**
	 * 获得用户组id和名称的map
	 * 
	 * @param id
	 * @return
	 */
	public Map<String, String> getMapByRoleId(Long id)
			throws WzServiceException {

		if (id == null) {
			throw new IllegalArgumentException("用户ID不能为空");
		}

		List<WzGroupEntity> list = this.wzGroupDao.listByUserId(id);

		if (list == null || list.size() == 0) {
			return null;
		}
		Map<String, String> map = new HashMap<String, String>();
		String ids = "";
		String names = "";

		for (Iterator<WzGroupEntity> it = list.iterator(); it.hasNext();) {
			WzGroupEntity entity = it.next();
			ids += "," + entity.getId();
			names += "," + entity.getName();
		}
		ids = ids.substring(1);
		names = names.substring(1);
		map.put("ids", ids);
		map.put("names", names);
		return map;

	}

	/**
	 * 获得激活的用户组列表
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public List<WzGroupSearch> listActive() throws WzWebException {

		WzGroupSearch searchEntity = new WzGroupSearch();
		searchEntity.setIsActive(1); // 激活的
		List<WzGroupSearch> list = this.listAll(searchEntity);
		return list;
	}

	/**
	 * 获得用户组列表
	 * 
	 * @param userId
	 * @return
	 * @throws WzServiceException
	 */
	public List<WzGroupEntity> listGroupByUserId(Long userId)
			throws WzServiceException {

		List<WzGroupEntity> list = this.wzGroupDao.listByUserId(userId);
		return list;
	}

	/**
	 * 保存或修改用户组
	 * 
	 * @param entity
	 * @param roleIds
	 * @throws WzServiceException
	 */
	public void saveOrUpdate(WzGroupEntity entity, String roleIds)
			throws WzServiceException {

		// 保存用户组
		this.wzGroupDao.saveOrUpdate(entity);

		// 删除角色用户组
		this.wzRoleGroupService.deleteByGroupId(entity.getId());

		// 保存角色用户组
		if (StringUtils.isNotEmpty(roleIds)) {
			String resIdStr[] = roleIds.split(",");
			for (String roleId : resIdStr) {

				WzRoleGroupEntity rescRoleEntity = new WzRoleGroupEntity();
				rescRoleEntity.setGroupId(Long.valueOf(roleId));
				rescRoleEntity.setRoleId(entity.getId());
				wzRoleGroupService.save(rescRoleEntity);
			}
		}
	}

	/**
	 * 删除用户组
	 * 
	 * @param idList
	 * @throws WzServiceException
	 */
	public void delete(Collection<Serializable> idList)
			throws WzServiceException {

		// 空就返回
		if (idList == null || idList.size() == 0) {
			return;
		}

		// 删除角色用户组
		for (Iterator<Serializable> it = idList.iterator(); it.hasNext();) {
			Long id = Long.valueOf(it.next().toString());
			this.wzRoleGroupService.deleteByGroupId(id);
		}

		// 删除角色
		this.deleteAll(idList);
	}

	public void setWzGroupDao(WzGroupDao wzGroupDao) {
		this.setBaseDao(wzGroupDao);
		this.wzGroupDao = wzGroupDao;
	}

	public void setWzRoleGroupService(WzRoleGroupService wzRoleGroupService) {
		this.wzRoleGroupService = wzRoleGroupService;
	}

}